"""
Case Type   : 资源池化
Case Name   : 系统函数query_page_distribution_info函数字段及功能验证
Create At   : 2024.6
Owner       : haomeng
Description :
    1、创建表，插入数据
    2、查询数据页面的分布信息(数据文件)
    3、查询数据页面的分布信息(FSM文件)
    4、查询数据页面的分布信息(visibility map文件)
    5、查询数据页面的分布信息(BCM文件)
    6、备节点查询页面分布信息
    7、清理环境
Expect      :
    1、创建表成功，插入数据成功
    2、数据文件查看成功
    3、FSM文件查看成功
    4、VM文件查看失败
    5、BCM文件查看失败
    6、查询失败，备节点不支持查询
    7、清理环境成功
"""


import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.table = 't_ss_inner_05'
        self.e_msg1 = 'ERROR:  [SS] cannot query query_page_distribution_info' \
                      ' at Standby node while DMS enabled!'
        self.e_msg2 = 'INFO:  buffer does not exist'

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_innerfunc(self):
        text = '-----step1:创建表，插入数据; expect:创建成功，插入数据成功-----'
        self.log.info(text)

        cmd = f"create table {self.table}(id int);" \
              f"insert into {self.table} values(generate_series(0,10000));"
        self.log.info(cmd)
        cre_tab = self.pri_sh.execut_db_sql(cmd)
        self.log.info(cre_tab)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, cre_tab, '建表失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, cre_tab, '插入数据失败')

        text = '-----step2:查询表数据页面的分布信息(数据文件); expect:数据文件查看成功-----'
        self.log.info(text)

        sql1 = f'''select * from query_page_distribution_info('{self.table}',0,0);'''
        self.log.info(sql1)
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertIn('(1 row)', res1, '查看表数据页面的分布信息成功')

        text = '-----step3:查询数据页面的分布信息(FSM文件); expect:数据文件查看成功-----'
        self.log.info(text)

        sql2 = f'''select * from query_page_distribution_info('{self.table}',1,0);'''
        self.log.info(sql2)
        res2 = self.pri_sh.execut_db_sql(sql2)
        self.log.info(res2)
        self.assertIn('(1 row)', res2, '查看表数据页面的分布信息成功')

        text = '-----step4:查询数据页面的分布信息(visibility map文件); expect:buffer不存在-----'
        self.log.info(text)

        sql3 = f'''select * from query_page_distribution_info('{self.table}',2,0);'''
        self.log.info(sql3)
        res3 = self.pri_sh.execut_db_sql(sql3)
        self.log.info(res3)
        self.assertIn(self.e_msg2, res3, 'buffer信息存在')

        text = '-----step5:查询数据页面的分布信息(visibility map文件); expect:buffer不存在-----'
        self.log.info(text)

        sql4 = f'''select * from query_page_distribution_info('{self.table}',3,0);'''
        self.log.info(sql4)
        res4 = self.pri_sh.execut_db_sql(sql4)
        self.log.info(res4)
        self.assertIn(self.e_msg2, res4, 'buffer信息存在')

        text = '-----step6:备节点查询页面分布信息; expect:查询失败，备节点不支持-----'
        self.log.info(text)

        res5 = self.sta_sh.execut_db_sql(sql1)
        self.log.info(res5)
        self.assertIn(self.e_msg1, res5, '备节点查询页面分布信息成功')

    def tearDown(self):
        text = '-----step7:清理环境  expect:清理成功-----'
        self.log.info(text)

        clean = self.pri_sh.execut_db_sql(f'drop table {self.table} cascade;')
        self.log.info(clean)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, clean, '删除表失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
